﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PowerCreator.LMS.Common.Script;

public partial class Theme_3_OpenCourseWare_content : PowerCreator.LMS.Web.Base.BasePage
{
    protected string sMMSPath = "";
    protected string sVGAPath = "";
    protected string filePath = string.Empty;
    PowerCreator.LMS.Web.Course.Resource op = new PowerCreator.LMS.Web.Course.Resource();
    PowerCreator.LMS.ViewQuery.ViewQuery opView = new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string Loginurl = "../../Default.aspx";
    protected PowerCreator.LMS.Web.Config.WebConfig configpage = new PowerCreator.LMS.Web.Config.WebConfig();
    protected void Page_Load(object sender, EventArgs e)
    {
        Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        CheckPage();
        CheckUser();

        if (!IsPostBack)
        {
            ViewState["ResourceID"] = Request.QueryString["rsd"].ToString();

            BinderPG();
        }
        //filePath = Request.QueryString["fileUrl"].ToString();
        //getPath(filePath);
    }


    private void CheckPage()
    {
        if (Request.UrlReferrer == null)
        {
            Script.RedirectURL(Loginurl);
        }
        if (Request.Url.Host != Request.UrlReferrer.Host)
        {
            Script.RedirectURL(Loginurl);
        }
    }
         
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(Loginurl);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(Loginurl);
        }

    }



    private void BinderPG()
    {
        //初始化界面的播放地址
        PowerCreator.LMS.Course.Entity.VOTResource voResource = opCourse.LoadResource(int.Parse(ViewState["ResourceID"].ToString()));

        if (voResource.ResourceID < 0)
        {
            Script.ShowAndCloseMe(Page, config.GetLanagerValueByKey("theme_Comment_OpenCourseware_Msg1", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }


        filePath = voResource.ResourceUrl;
        //fileUrl = "../Single/content.aspx?fileUrl=" + fileUrl;
        // _fileUrl = string.Format("../Single/content.aspx?ProjectID={0}&ClassID={1}&UserID={2}&ProjectResourceID={3}&CoursResourceID={4}&ResourceID={5}&Position={6}&CourseID={7}", ViewState["ProjectID"], ViewState["ProjectClassID"], GetLoginEntity.UserID, ViewState["ProjectResourceID"], ViewState["CourseResourceID"], ViewState["ResourceID"], _Position, ViewState["CourseID"]);


        string url = Request.Url.Authority.ToString();
        sVGAPath = "http://" + url + filePath + "Screen.vga";


        if (PowerCreator.LMS.Web.Config.WebConfig.IsUseURLIP)
        {
            string sDrictoyName = op.GetDirctoryName(filePath);
            string search = "select VirId,VirIisName,VirMmsName,VirIp from TVirtualDirectory where VirIisName='{0} '";
            DataTable dt = opView.Query(string.Format(search, sDrictoyName));
            if (dt.Rows.Count == 1)
            {
                string sResourceMMSUrl = filePath.Replace(sDrictoyName, dt.Rows[0]["VirMmsName"].ToString());
                //sMMSPath = "mms://" + dt.Rows[0]["VirIp"].ToString() + "/" + sResourceMMSUrl + "/000.asf";
                string[] urls = url.Split(':');
                string ServerIP = urls[0];

                sMMSPath = "mms://" + ServerIP + "/" + sResourceMMSUrl + "/000.asf";
                //sMMSPath = "mms://" + dt.Rows[0]["VirIp"].ToString() + "/" + sResourceMMSUrl+"/000.asf";

            }
            else
            {
                sMMSPath = "http://" + url + filePath + "remoteclip.asx";
            }

        }
        else
        {
            string sDrictoyName = op.GetDirctoryName(filePath);
            string search = "select VirId,VirIisName,VirMmsName,VirIp from TVirtualDirectory where VirIisName='{0} '";
            DataTable dt = opView.Query(string.Format(search, sDrictoyName));
            if (dt.Rows.Count == 1)
            {
                string sResourceMMSUrl = filePath.Replace(sDrictoyName, dt.Rows[0]["VirMmsName"].ToString());
                sMMSPath = "mms://" + dt.Rows[0]["VirIp"].ToString() + "/" + sResourceMMSUrl + "/000.asf";
                //sMMSPath = "mms://" + dt.Rows[0]["VirIp"].ToString() + "/" + sResourceMMSUrl+"/000.asf";

            }
            else
            {
                sMMSPath = "http://" + url + filePath + "remoteclip.asx";
            }


        }

    }



}
